package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {

        if (member.getPassword()!=null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }

        memberMapper.add(member);
    }

    /**
     * 获取过去12个月，每月的会员统计数量
     * @return
     */
    @Override
    public Map getMemberReport() {

        //初始化日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历对象往前推12个月
        calendar.add(Calendar.MONTH,-12);

        //将过去12个月的月份存入集合中
        List<String> months = new ArrayList<>();

        //将过去12个月每个月注册的会员数量存入一个集合中
        List<Integer> memberCounts = new ArrayList<>();

        for (int i = 0; i < 12; i++) {

            //获取日历对象的当前日期
            Date time = calendar.getTime();
            //将日期装换为固定的格式：2018-09
            String date = new SimpleDateFormat("yyyy-MM").format(time);
            //将过去12个月的月份存入集合中
            months.add(date);

            //定义每个月的第一天
            String dateBegin=date + "-01";
            //定义每个月的最后一天
            String dateEnd=date + "-31";

            //查询每个月的会员数量
            Integer count = memberMapper.findMemberCountsByDate(dateBegin,dateEnd);
            memberCounts.add(count);
            //将日历对象加上一个月，-12月，-11月，-10月
            calendar.add(Calendar.MONTH,1);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCounts",memberCounts);

        return map;
    }

    /**
     * 查询男女占比
     * @return
     */
    @Override
    public Map getGenderReport() {

        List<Map> list = memberMapper.getGenderReport();

        List<Map> maps = new ArrayList<>();

        //定义一个集合，用来存放所有的gender
        List<String> genders = new ArrayList<>();

        for (Map map : list) {
            String gender = (String) map.get("name");

            if ("1".equalsIgnoreCase(gender) ) {
                gender = "男";
            } else if ("2".equalsIgnoreCase(gender) ) {
                gender = "女";
            } else {
                gender = "未透露";
            }
            map.put("value", map.get("value"));
            map.put("name", gender);

            genders.add(gender);

            maps.add(map);
        }


        //就是前台需要解析的数据
        Map<String, Object> map = new HashMap<>();

        map.put("genders",genders);
        map.put("genderCounts",maps);

        return map;
    }

    /**
     * 根据身份证号查询年龄 占比
     * @return
     */
    @Override
    public Map getAgeReport() {

        /*数据库中查询生日*/
        List<String> idCards  = memberMapper.findIdCard();

        /*0-18*/
        int count1 = 0;
        /*18-30*/
        int count2 = 0;
        /*30-45*/
        int count3 = 0;
        /*>45*/
        int count4 = 0;
        /*无记录*/
        int count5 = 0;

        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        Integer year= Integer.parseInt(df.format(new Date()));

        /*获取当前时间，进行对比，随后进行判断在哪个年龄区间内*/
        for (String idCard : idCards) {
            if (idCard ==null) {
                count5++;
                continue;
            }
            Integer birthyear = Integer.parseInt(idCard.substring(6, 10));

            int age = year - birthyear;
            if (0 < age && age <= 18) {
                count1++;
            } else if (age>18&&age<=30) {
                count2++;
            } else if (age>30&& age<=45) {
                count3++;
            } else if (age > 45) {
                count4++;
            }
        }
        List<Integer> counts = new ArrayList<>();
        counts.add(count1);
        counts.add(count2);
        counts.add(count3);
        counts.add(count4);
        counts.add(count5);

        /*保存所有的年龄段*/
        List<String> ages = new ArrayList<>();
        ages.add("0-18");
        ages.add("18-30");
        ages.add("30-45");
        ages.add("大于45");
        ages.add("未透露");

        /*用来保存所有的年龄对应的值*/
        List<Map> ageCounts = new ArrayList<>();



        for (int i = 0; i < 5; i++) {
            Map<Object, Object> map1 = new HashMap<>();
            map1.put("value", counts.get(i));
            map1.put("name", ages.get(i));
            ageCounts.add(map1);

        }


        Map<String, Object> map = new HashMap<>();

        map.put("ages",ages);
        map.put("ageCounts",ageCounts);

        return map;
    }

    public static void main(String[] args) {

        //初始化日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历对象往前推12个月
        calendar.add(Calendar.MONTH,-12);

        Date time = calendar.getTime();

        String date = new SimpleDateFormat("yyyy-MM").format(time);

        System.out.println(date);

    }


}
